<?php

/**
 * Group File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Group
 * @package  Admin_Models_Data_Group_Package
 * @author Johari <joharijumali@gmail.com>  
 */
namespace Admin\Models\Data;

use \Laravel\Database\Eloquent\Model as Eloquent;
use Datagrid;
use Laravel\Config as Config;
use Laravel\Database as DB;
use Laravel\Log;
use Laravel\Str;
use Laravel\Lang;
use Admin\Models\User\Profile;

/**
 * Data Group Model Class
 *
 * Create model class for lookup data
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Group extends Eloquent
{

	public static $timestamps = true;
    public static $table = 'data_group';
    public static $key = 'dmid';

	/**
     * Function listData
	 *
	 * @return listData.
     */
    public static function listData()
	{
        $allGroup = Group::paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;
        $datagrid->setFields(array('group_name' => Str::upper(Lang::line('admin.datagroup')->get())));
        $datagrid->setAction(Lang::line('global.manage')->get(), 'datacontent', false);
        $datagrid->setAction(Lang::line('global.edit')->get(), 'editGroup', true, array('dmid'));
        $datagrid->setAction(Lang::line('global.delete')->get(), 'deleteGroup', true, array('dmid', 'group_name'));
        $datagrid->setTable('dataGroup', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->build($allGroup, 'dmid');

        return $datagrid->render();
    }
	
	/**
     * Function getGroupinfo that pass parameter
     *
     * @param integer $groupID the integer to quote
	 *
	 * @return parameter.
     */
    public static function getGroupinfo($groupID)
	{
        $dataGroup = Group::find($groupID);

        $data['groupid'] = $dataGroup->dmid;
        $data['group_name'] = $dataGroup->group_name;
        $data['group_model'] = $dataGroup->group_model;
        $data['group_key'] = $dataGroup->group_key;

        return $data;
    }
	
	/**
     * Function getDataModel that pass parameter
     *
     * @param integer $groupID the integer to quote
	 *
	 * @return parameter.
     */
    public static function getDataModel($groupID)
	{
        $dataGroup = Group::find($groupID);

        Content::$table = $dataGroup->group_model;
        Content::$key = $dataGroup->group_key;
        Content::$timestamps = 'true';
        Content::$header = $dataGroup->group_name;
    }

	/**
     * Function setDataInput that pass parameter
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return parameter.
     */
    public static function setDataInput($input)
	{
    	try {
    		Content::setData($input);
    	} catch (Exception $e) {
            Log::write('Data', 'Data Insertion Not Success');
     }
    }
	
	/**
     * Function remDataInput that pass parameter
     *
     * @param integer $dataId   the integer to quote
     * @param varchar $dataName the varchar to quote
	 *
	 * @return parameter.
     */
    public static function remDataInput($dataId, $dataName)
	{
    	try {
    		Content::remData($dataId, $dataName);
    	} catch(Exception $e) {
            Log::write('Data', 'Data Removal Not Success');
     }
    }
	
	/**
     * Function getDataList
	 *
	 * @return getDataList.
     */
    public static function getDataList()
	{
        return Content::genListData();
    }

	/**
     * Function getDataInfo that pass parameter
     *
     * @param integer $id the integer to quote
	 *
	 * @return parameter.
     */
    public static function getDataInfo($id)
	{
        return Content::getInfo($id);
    }
	
	/**
     * Function getDataHeader
	 *
	 * @return getDataHeader.
     */
    public static function getDataHeader()
	{
        return Content::$header;
    }
	
	/**
     * Check Existing Data
     *
     * @param varchar $table the varchar to quote
	 *
	 * @return void.
     */
    public static function checkData($table = null)
	{
        $existTable = DB::query("show tables like '".Content::$table."'");

        if (empty($existTable)) {
			return false;
        }

        $table = ($table != null)?$table:Content::$table;
        $count = DB::table($table)->count();

        return ($count>0)?true:false;
    }
	
	/**
     * Check Existing table
     *
     * @param varchar $table the varchar to quote
     * @param varchar $field the varchar to quote
	 *
	 * @return void.
     */
    public static function checkTable($table, $field)
	{
        $existTable = DB::query("show tables like '".$table."'");
        if (!empty($existTable)) {
			$tableCol = DB::query("show columns from ".$table." like '".$field."'");
        }

        return (!empty($existTable) && !empty($tableCol))?true:false;
    }
	
	/**
     * Custom Data Validation
     *
     * @param integer $id the integer to quote
	 *
	 * @return void.
     */
    public static function validateData($id)
	{
        switch (Content::$table) {
        case 'data_jabatan':
            $bil = Profile::where('jabatanid', '=', $id)->count();
            break;

        case 'data_unit':
            $bil = Profile::where('unitid', '=', $id)->count();
            break;

        case 'data_bahagian':
            $bil = Profile::where('bahagianid', '=', $id)->count();
            break;
            
        default:
            $bil = 0;
            break;
        }

        if ($bil > 0 ) {
            return false;
        } else {
            return true;
        }
    }

}

?>